//
//  NetworkTools.swift
//  Weibo
//
//  Created by 明丽 on 16/1/21.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit
import AFNetworking
//使用网络请求获取单利对象
private let WBDomain = "com.weibo.data.error"
class NetworkTools: AFHTTPSessionManager {
    //网络请求单利对象
    static let shardTools: NetworkTools = {
        //一个完整的URL = badeURl + 接口名称
        let urlstring = "https://api.weibo.com/"
        let url = NSURL(string: urlstring)
        let tools = NetworkTools(baseURL: url)
        //设置反序列化格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
        
    }()
    
    //定一个网络请求的核心方法 以后所有的网络请求都是通过这个方法 来和AFN直接联系
    func requestJSONDict(urlString: String,paraneters: [String : AnyObject]?,finished: (dict: [String: AnyObject]?, error: NSError?)-> ()){
        
        POST(urlString, parameters: paraneters, progress:nil, success: { (_, result) -> Void in
            
            guard let dict = result as? [String : AnyObject] else {
            print("数据加载错误")
                let myError = NSError(domain: WBDomain, code: -999, userInfo: [NSLocalizedDescriptionKey:"输入的数据不合法"])
            
                
                finished(dict:nil, error: myError)
                return
            }
            
            finished(dict: dict, error: nil)
            
            }) { (_, error) -> Void in
                
                finished(dict:nil, error: error)
                print(error)
        }
        
    }
    
    
    
    
}
